package com.spc.car.data.service.org.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spc.car.common.utils.StringUtils;
import com.spc.car.data.entity.org.OrgQualification;
import com.spc.car.data.mapper.org.OrgQualificationMapper;
import com.spc.car.data.domain.org.po.GetOrgQualificationsListPO;
import com.spc.car.data.domain.org.po.SaveOrgQualificationsPO;
import com.spc.car.data.service.org.OrgQualificationService;
import com.spc.car.data.domain.org.vo.OrgQualificationListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 机构资质 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-03-27
 */
@Service
public class OrgQualificationServiceImpl extends ServiceImpl<OrgQualificationMapper, OrgQualification> implements OrgQualificationService {

    @Override
    public int saveOrgQualification(SaveOrgQualificationsPO saveOrgQualificationsPO) {
        OrgQualification orgQualification = new OrgQualification();
        BeanUtils.copyProperties(saveOrgQualificationsPO ,orgQualification);
        if (null == saveOrgQualificationsPO.getId() || 0 == saveOrgQualificationsPO.getId()){
            orgQualification.setCreateTime(new Date());
        }else {
            orgQualification.setModifyTime(new Date());
        }
        if (saveOrUpdate(orgQualification)){
            return 1;
        }
        return 0;
    }

    @Override
    public Page<OrgQualificationListVO> selectOrgQualificationList(GetOrgQualificationsListPO getOrgQualificationsListPo) {
        Page page = page(getOrgQualificationsListPo.getPage(), new QueryWrapper<OrgQualification>()
                .eq(null != getOrgQualificationsListPo.getId(), "id", getOrgQualificationsListPo.getId())
                .like(StringUtils.isNotBlank(getOrgQualificationsListPo.getOrgCode()), "org_code", getOrgQualificationsListPo.getOrgCode())
                .like(StringUtils.isNotBlank(getOrgQualificationsListPo.getQualCode()), "qual_code", getOrgQualificationsListPo.getQualCode())
                .like(StringUtils.isNotBlank(getOrgQualificationsListPo.getDeclPrincipal()), "decl_principal", getOrgQualificationsListPo.getDeclPrincipal())
        );
        if (page.getRecords().isEmpty()){
            return page;
        }
        List<OrgQualificationListVO> orgQualificationListVOS = new ArrayList<>();
        page.getRecords().stream().forEach(anorgQualification ->{
            OrgQualificationListVO orgQualificationListVo = new OrgQualificationListVO();
            BeanUtils.copyProperties(anorgQualification, orgQualificationListVo);
            orgQualificationListVOS.add(orgQualificationListVo);
        });

        return page.setRecords(orgQualificationListVOS);
    }
}
